/**
 * Created by waltyuqing on 16/12/21.
 */

 $('.news_option_sel').change(
     function(){
         var option=$('.news_option_sel option:selected').text();
         $.ajax({
             type: 'post',
             url: "../api/message_manage/news/change_news.jsp",
             data: {
                  labelId:$(this).val(),
             },
             timeout: 3000,
             dataType: 'json',
             success:function (data) {
                 console.log(data);
                 if(data.errcode==0) {
                     $('.split_pages').empty();
                     $('.department_detail_table').empty()
                     $('.department_detail_table').append(
                         '<tr class="department_detail_tittle">'+
                         '<td class="ui_widther">标题</td>'+
                         '<td class="ui_widthone">发表日期</td>'+
                         '<td class="ui_widthone">作者</td>'+
                         '<td class="ui_widthtwo">操作</td>'+
                         '<td class="ui_widthone">是否选为首页轮播</td>'+
                         '</tr>'
                     );
                     for(var i=0;i<data.data.rank.length;i++){
                         $('.department_detail_table').append(
                             '<tr class="department_detail_c" >'+
                             '<td>'+data.data.rank[i].title+' </td>'+
                             '<td>'+data.data.rank[i].time+' </td>'+
                             '<td>'+data.data.rank[i].author+'</td>'+
                             "<td class='ui_color'>"+
                             '<span class="mes_doc_edit ui_color_span"><a href="hospital_news_see.jsp?news_id='+data.data.rank[i].id+'&option='+option+'">查看</a></span>'+
                             '<span class="mes_doc_edit doctor_add_btn ui_color_span"><a href="hospital_news_edit.jsp?news_id='+data.data.rank[i].id+'&option='+option+'">编辑</a></span>'+
                             '<span class="delete_doc_mes ui_color_span" id="'+data.data.rank[i].id+'">删除</span>' +
                             '</td>'+
                             '<td><input name="choose_home_picture" type="checkbox"  onclick="checkboxOnclick(this)"   value="'+data.data.rank[i].id+'"/></td>'+
                             '</tr>'                                )
                     };
                     $('.split_pages').append(
                         '<div class="record">共<span class="ui_red">'+data.data.count+'</span>记录</div>'+
                         '<div class="tcdPageCode holder"></div>'+
                         '<input type="hidden" name="pageCount" value='+data.data.page_count+'>'+
                         '<input type="hidden" name="current_page" value='+data.data.current_page+'>'
                     );
                     var page=parseInt($("input[name='pageCount']").val());
                     var current_page=parseInt($("input[name='current_page']").val());
                     var labelId= $('.news_option_sel option:selected').val();
                     $(".tcdPageCode").createPage({
                             pageCount:page,
                             current:current_page,
                         backFn:function(p){
                         $.ajax({
                             type: 'post',
                             url: "../api/message_manage/news/change_news.jsp",
                             data: {
                                 page:p,
                                 labelId:labelId,

                             },
                             timeout: 3000,
                             dataType: 'json',
                             success:function (data) {
                                 if(data.errcode==0){
                                     $('.department_detail_table').empty()
                                     $('.department_detail_table').append(
                                         '<tr class="department_detail_tittle">'+
                                         '<td class="ui_widther">标题</td>'+
                                         '<td class="ui_widthone">发表日期</td>'+
                                         '<td class="ui_widthone">科室名称</td>'+
                                         '<td class="ui_widthtwo">操作</td>'+
                                         '<td class="ui_widthone">是否选为首页轮播</td>'+
                                         '</tr>'
                                     );
                                     for(var i=0;i<data.data.rank.length;i++){
                                         $('.department_detail_table').append(
                                             '<tr class="department_detail_c">'+
                                             '<td>'+data.data.rank[i].title+'</td>'+
                                             '<td>'+data.data.rank[i].time+'</td>'+
                                             '<td>'+data.data.rank[i].author+'</td>'+
                                             "<td class='ui_color'>"+
                                             '<span class="mes_doc_edit doctor_add_btn ui_color_span"><a href="hospital_news_see.jsp?news_id='+data.data.rank[i].id+'&option='+$('.news_option_sel option:selected').text()+'">查看</a></span>'+
                                             '<span class="mes_doc_edit doctor_add_btn ui_color_span"><a href="hospital_news_edit.jsp?news_id='+data.data.rank[i].id+'&option='+$('.news_option_sel option:selected').text()+'">编辑</a></span>'+
                                             '<span class="delete_doc_mes ui_color_span" id="'+data.data.rank[i].id+'">删除</span>'+
                                             '</td>'+
                                             '<td><input name="choose_home_picture" type="checkbox"  onclick="checkboxOnclick(this)"   value="'+data.data.rank[i].id+'"/></td>'+
                                             '</tr>'
                                         );
                                     };
                                 }},
                             error:function (error) {

                             }
                         });
                         return false;//阻止提交

                     }
                 });

                 }else{
                     $('.split_pages').empty();
                     $('.department_detail_table').empty();
                     $('.department_detail_table').append(
                         '<tr class="department_detail_tittle">'+
                         '<td class="ui_widththree">标题</td>'+
                         '<td class="ui_widthone">发表日期</td>'+
                         '<td class="ui_widthone">科室名称</td>'+
                         '<td class="ui_widthtwo">操作</td>'+
                         '<td class="ui_widthone">是否选为首页轮播</td>'+
                         '</tr>'
                     );
                     $('.department_detail_table').append(
                         '<div>暂无相关数据</div>'
                     )
                     //swal('没有相关新闻列表')
                 }
             },
             error:function (error) {

                 //swal('没有相关新闻列表')
             }
         });
         return false;//阻止提交
     }
 )
//删除新闻
$('body').on('click','.delete_doc_mes',

    function(){
        var news_id=$(this).attr('id');
        var this_html=$(this).parent().parent()
        swal({
                title: "您确定要删除吗？",
                type: "warning",
                showCancelButton: true,
                closeOnConfirm: false,
                confirmButtonText: "是的，我要删除",
                cancelButtonText:"取消",
                confirmButtonColor: "#ec6c62"
             },
        function(){
            $.ajax({
                type: 'post',
                url: "../api/message_manage/news/delete_news.jsp",
                data: {
                    news_id:news_id,
                },
                timeout: 3000,
                dataType: 'json',
                success:function (data) {
                    if(data.errcode==0){
                        // self.location.reload();
                        swal('删除成功')
                        this_html.empty()
                    }else{
                        swal('删除失败')
                    }

                    //console.log(data)
                },
                error:function (error) {
                    swal('删除失败')
                }
            });
            return false;//阻止提交
        }
        );
    }
)
